package com.xiatl.cgi.server.cgi;

import com.xiatl.cgi.server.cgi.exception.CgiException;
import com.xiatl.cgi.server.cgi.handler.ICgiHandler;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class CgiHandler implements InitializingBean {
    private List<ICgiHandler> handlers = new ArrayList<>();

    public void doHandle(CgiDataModel dataModel) throws CgiException {
        for (ICgiHandler handler : handlers) {
            handler.handle(dataModel);
        }
    }

    @Override
    public void afterPropertiesSet() {
        // 限流处理
        handlers.add(this.cgiLimitingHandler);
        // 签名验证过滤器
        handlers.add(this.cgiSignHandler);
        // 计费处理
        handlers.add(this.cgiBillingHandler);
    }

    @Autowired
    private ICgiHandler cgiBillingHandler;
    @Autowired
    private ICgiHandler cgiLimitingHandler;
    @Autowired
    private ICgiHandler cgiSignHandler;
}
